<%--
  Created by IntelliJ IDEA.
  User: MadFrog
  Date: 2016/11/22
  Time: 9:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<html>
<head>
    <title>创建投票</title>
    <%@include file="../common/_head.jsp" %>
</head>
<body ontouchstart>

<form action="elect/add" method="post">
    <div class="weui-cell">
        <div class="weui-cell__bd">
            <input class="weui-input" placeholder="请输入标题" name="title" type="text">
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__bd">
            <textarea class="weui-textarea" placeholder="投票说明，请输入文字" rows="3" name="description"></textarea>
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__hd"><label for="closeDate" class="weui-label">截止时间</label></div>
        <div class="weui-cell__bd">
            <input class="weui-input" id="closeDate" name="closeDate" type="text">
        </div>
    </div>
    <div class="weui-cell weui-cell_switch">
        <div class="weui-cell__bd">允许弃权</div>
        <div class="weui-cell__ft">
            <input type="checkbox" class="weui-switch" name="allowAbstain" value="YES">
        </div>
    </div>
    <div class="weui-cell">
        <div class="weui-cell__bd">
            <p>投票数量</p>
        </div>
        <div class="weui-cell__ft">
            <div class="weui-count">
                <a class="weui-count__btn weui-count__decrease"></a>
                <input type="number" class="weui-count__number" value="3" name="electedNumber">
                <a class="weui-count__btn weui-count__increase"></a>
            </div>
        </div>
    </div>

    <div class="weui-loadmore weui-loadmore_line">
        <span class="weui-loadmore__tips">候选人</span>
    </div>
    <div class="weui-cells weui-cells_radio">
        <label class="weui-cell weui-check__label" for="candidate_allStaff">
            <div class="weui-cell__bd">
                <p>全体教工</p>
            </div>
            <div class="weui-cell__ft">
                <input type="radio" class="weui-check" name="candidate" id="candidate_allStaff" checked="checked"
                       value="1">
                <span class="weui-icon-checked"></span>
            </div>
        </label>
        <label class="weui-cell weui-check__label" for="candidate_inFormal">
            <div class="weui-cell__bd">
                <p>编内教工</p>
            </div>
            <div class="weui-cell__ft">
                <input type="radio" class="weui-check" name="candidate" id="candidate_inFormal" value="2">
                <span class="weui-icon-checked"></span>
            </div>
        </label>
        <label class="weui-cell weui-check__label" for="candidate_outFormal">
            <div class="weui-cell__bd">
                <p>外聘教工</p>
            </div>
            <div class="weui-cell__ft">
                <input type="radio" class="weui-check" name="candidate" id="candidate_outFormal" value="3">
                <span class="weui-icon-checked"></span>
            </div>
        </label>
        <label class="weui-cell weui-check__label" for="candidate_isCommunist">
            <div class="weui-cell__bd">
                <p>党员</p>
            </div>
            <div class="weui-cell__ft">
                <input type="radio" class="weui-check" name="candidate" id="candidate_isCommunist" value="4">
                <span class="weui-icon-checked"></span>
            </div>
        </label>
        <a href="javascript:void(0);" class="weui-cell weui-cell_link">
            <div class="weui-cell__bd">手动选择</div>
        </a>
    </div>

    <div class="weui-loadmore weui-loadmore_line">
        <span class="weui-loadmore__tips">投票人</span>
    </div>
    <div class="weui-cells weui-cells_radio">
        <label class="weui-cell weui-check__label" for="voter_allStaff">
            <div class="weui-cell__bd">
                <p>全体教工</p>
            </div>
            <div class="weui-cell__ft">
                <input type="radio" class="weui-check" name="voter" id="voter_allStaff" checked="checked" value="1">
                <span class="weui-icon-checked"></span>
            </div>
        </label>
        <label class="weui-cell weui-check__label" for="voter_inFormal">
            <div class="weui-cell__bd">
                <p>编内教工</p>
            </div>
            <div class="weui-cell__ft">
                <input type="radio" class="weui-check" name="voter" id="voter_inFormal" value="2">
                <span class="weui-icon-checked"></span>
            </div>
        </label>
        <label class="weui-cell weui-check__label" for="voter_outFormal">
            <div class="weui-cell__bd">
                <p>外聘教工</p>
            </div>
            <div class="weui-cell__ft">
                <input type="radio" class="weui-check" name="voter" id="voter_outFormal" value="3">
                <span class="weui-icon-checked"></span>
            </div>
        </label>
        <label class="weui-cell weui-check__label" for="voter_isCommunist">
            <div class="weui-cell__bd">
                <p>党员</p>
            </div>
            <div class="weui-cell__ft">
                <input type="radio" class="weui-check" name="voter" id="voter_isCommunist" value="4">
                <span class="weui-icon-checked"></span>
            </div>
        </label>
        <a href="javascript:void(0);" class="weui-cell weui-cell_link">
            <div class="weui-cell__bd">手动选择</div>
        </a>
    </div>
    <div style="text-align:center">
        <a href="javascript:;" onclick="send()" class="weui-btn weui-btn_primary weui-btn_medium">保存</a>
    </div>
</form>

<%@include file="../common/_foot.jsp" %>
<script>
    $(document).ready(function () {
        var MAX = 99, MIN = 1;
        $('.weui-count__decrease').click(function (e) {
            var $input = $(e.currentTarget).parent().find('.weui-count__number');
            var number = parseInt($input.val() || "0") - 1
            if (number < MIN) number = MIN;
            $input.val(number)
        });
        $('.weui-count__increase').click(function (e) {
            var $input = $(e.currentTarget).parent().find('.weui-count__number');
            var number = parseInt($input.val() || "0") + 1
            if (number > MAX) number = MAX;
            $input.val(number)
        });
        $("#closeDate").datetimePicker();
    });
    function send() {
        // $('.weui-btn_medium').removeAttr('onclick');
        var params = {};
        params.title = $("input[name='title']").val();
        params.description = $("textarea[name='description']").val();
        params.closeDate = $("input[name='closeDate']").val();
        params.allowAbstain = $("input[name='allowAbstain']").val();
        params.electedNumber = $("input[name='electedNumber']").val();
        params.candidate = $("input[name='candidate']:checked").val();
        params.voter = $("input[name='voter']:checked").val();

        if(params.title == null || params.title == undefined || params.title == ''){
            $.toptip('标题不能为空', 'error');
            return;
        }
        if(params.description == null || params.description == undefined || params.description == ''){
            $.toptip('说明不能为空', 'error');
            return;
        }
        if(params.closeDate == null || params.closeDate == undefined || params.closeDate == ''){
            $.toptip('截止时间不能为空', 'error');
            return;
        }
        $.ajax({
            type: "post",
            url : "${ctx}/teacher/elect/add",
            data : params,
            dataType : "json",
            success: function (re) {
                if(true==re.success){
                    $.toast("操作成功", function () {
                        window.location = "index";
                    });
                }else {
                    $.toast("后台错误，请联系管理员", "cancel");
                }
            },
            error: function (re) {
                $.toast("网络请求失败，请联系管理员", "cancel");
            }
        });
    }
</script>
</body>
</html>
